package com.itheima.chapter11.tcp.demo;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/*
收发数据
 */
public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket server =new ServerSocket(8899);
        System.out.println("服务端开启，等待客户端链接...");
        //响应客户发送的请求
        Socket socket =server.accept();
        //获取传输数据的流对象
        InputStream is = socket.getInputStream();
        OutputStream os = socket.getOutputStream();
        //传输数据
        byte[] bys =new byte[1024];
        int len = is.read(bys);
        String msg = new String(bys, 0, len);
        String ip = socket.getInetAddress().getHostAddress();
        System.out.println("读到:"+ip+"发送信息:"+msg);

        os.write("很高兴遇见你".getBytes());

        socket.close();
        server.close();
    }
}
